package com.qiantu.phone.widget.setting;

import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.base.BaseActivity;
import com.hjq.base.DensityUtils;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.activity.SensitivitySettingActivity;

import static android.app.Activity.RESULT_OK;
import static android.util.TypedValue.COMPLEX_UNIT_PX;

public class DeviceSettingInductionScope extends SettingBar implements View.OnClickListener {
    private DeviceBean mDevice;
    private AppActivity mContext;
    private Integer sensitivity;

    public DeviceSettingInductionScope(AppActivity context, DeviceBean deviceBean, String title) {
        this(context, null, deviceBean, title);
    }

    public DeviceSettingInductionScope(AppActivity context, AttributeSet attrs, DeviceBean deviceBean, String title) {
        this(context, attrs, 0, deviceBean, title);
    }

    public DeviceSettingInductionScope(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean, String title) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean, title);
    }

    public DeviceSettingInductionScope(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean, String title) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mContext = context;
        setLeftText(title);
        setRightTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor2));
        sensitivity = mDevice.getStateData().getSettings().getSensitivity();
        if(sensitivity != null){
            String text = sensitivity + "s";
            setRightText(text);
        }
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setRightTextSize(COMPLEX_UNIT_PX, DensityUtils.dip2px(context, 12));
        setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(mContext, SensitivitySettingActivity.class);
        intent.putExtra("value", sensitivity == null ? 0 : sensitivity);
        intent.putExtra("deviceSerialNo", mDevice.getDeviceSerialNo());
        intent.putExtra("productModel", mDevice.getProductModel());
        mContext.startActivityForResult(intent, new BaseActivity.OnActivityCallback() {
            @Override
            public void onActivityResult(int resultCode, Intent data) {
                if (resultCode == RESULT_OK) {
                    sensitivity = data.getIntExtra("sensitivity",0);
                    setRightText(sensitivity + "s");
                }
            }
        });
    }
}
